package cn.com.git.admin.service.impl;

import cn.com.git.admin.common.UpmsConstant;
import cn.com.git.admin.common.UpmsResultConstant;
import cn.com.git.admin.dao.*;
import cn.com.git.admin.dao.model.*;
import cn.com.git.admin.service.UpmsService;
import cn.com.git.admin.service.dto.*;
import cn.com.git.admin.service.query.*;
import cn.com.git.common.base.BaseServiceImpl;
import cn.com.git.common.model.ResultConstant;
import cn.com.git.common.model.ResultMsg;
import cn.com.git.common.model.ResultPage;
import cn.com.git.common.util.object.JSONUtils;
import cn.com.git.common.util.object.MapperFacadeUtils;
import cn.com.git.common.util.security.CodecUtils;
import cn.com.git.common.util.security.JwtUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* 鉴权 ServiceImpl 接口
* @author zhangzhenfu
* @Description
* @date 2020/5/8
*/
@Transactional
@Service
public class UpmsServiceImpl extends BaseServiceImpl implements UpmsService {

    @Autowired
    private SysUserDAO sysUserDAO;
    @Autowired
    private SysUserResourceDAO sysUserResourceDAO;
    @Autowired
    private SysRoleDAO sysRoleDAO;
    @Autowired
    private SysOrganDAO sysOrganDAO;
    @Autowired
    private SysDepartmentDAO sysDepartmentDAO;

    /*=================================*/
    /*============ 鉴权 =============*/
    /*=================================*/
    @Override
    public ResultMsg<AuthenticationDTO> loginByPassword(String account, String password) {

        if(StringUtils.isEmpty(account)){
            return new ResultMsg<>(UpmsResultConstant.EMPTY_USERNAME,"账号为空",null);
        }
        if(StringUtils.isEmpty(password)){
            return new ResultMsg<>(UpmsResultConstant.EMPTY_PASSWORD,"密码为空",null);
        }

        SysUserListQuery query1 = new SysUserListQuery();
        query1.setAccount(account);
        SysUser sysUser = sysUserDAO.selectOne(query1);
        if(sysUser==null){
            return new ResultMsg<>(UpmsResultConstant.INVALID_ACCOUNT,"用户不存在",null);
        }
        if(!password.equals(sysUser.getPassword())){
            return new ResultMsg<>(UpmsResultConstant.INVALID_PASSWORD,"密码错误",null);
        }

        SysUserResourceListQuery query2 = new SysUserResourceListQuery();
        query2.setSysUserId(sysUser.getId());
        List<SysUserResource> sysResourceList = sysUserResourceDAO.selectList(query2);
        //组装信息
        AuthenticationDTO authenticationDTO = new AuthenticationDTO();
        authenticationDTO.setSysUser(MapperFacadeUtils.map(sysUser,AuthenticationDTO.SysUser.class));
        if(CollectionUtils.isNotEmpty(sysResourceList)){
            List<Integer> ids = sysResourceList.stream().map(SysUserResource::getResourceId).collect(Collectors.toList());
            authenticationDTO.setResourceIdList(ids);
        }
        return new ResultMsg<>(ResultConstant.SUCCESS,null,authenticationDTO);
    }

    /*=================================*/
    /*============ SysUser =============*/
    /*=================================*/
    @Override
    public ResultPage<SysUserListDTO> listSysUser(SysUserListQuery query) {
        List<SysUserListDTO> data = new ArrayList<>();
        List<SysUser> list = sysUserDAO.selectList(query);
        Optional.ofNullable(list).orElse(Lists.newArrayList())
                .stream().forEach(item->{
            SysUserListDTO d = new SysUserListDTO();
            d.setM(MapperFacadeUtils.map(item,SysUserListDTO.SysUser.class));
            data.add(d);
        });
        return new ResultPage(ResultConstant.SUCCESS,data,data.size());
    }

    @Override
    public ResultMsg<SysUserDetailDTO> findSysUser(Integer id) {
        SysUserDetailDTO d = new SysUserDetailDTO();
        SysUser sysUser = sysUserDAO.selectById(id);
        d.setM(MapperFacadeUtils.map(sysUser,SysUserDetailDTO.SysUser.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,d);
    }

    @Override
    public ResultMsg<SysUserSaveDTO> addSysUser(SysUserSaveDTO saveDTO) {
        Date date = new Date();
        SysUser sysUser = MapperFacadeUtils.map(saveDTO.getM(),SysUser.class);
        if(sysUser.getPassword()!=null){
            sysUser.setPassword(CodecUtils.MD5(sysUser.getPassword()));
        }
        sysUser.setCreateTime(date);
        sysUser.setUpdateTime(date);
        Integer id = sysUserDAO.insertOne(sysUser);
        saveDTO.getM().setId(id);
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg<SysUserSaveDTO> editSysUser(SysUserSaveDTO saveDTO) {
        Date date = new Date();
        SysUser sysUser = MapperFacadeUtils.map(saveDTO.getM(),SysUser.class);
        if(sysUser.getPassword()!=null){
            sysUser.setPassword(CodecUtils.MD5(sysUser.getPassword()));
        }
        sysUser.setUpdateTime(date);
        sysUserDAO.updateOne(sysUser);
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg deleteSysUser(Integer id) {
        sysUserDAO.deleteOne(id);
        return new ResultMsg(ResultConstant.SUCCESS);
    }


    @Override
    public ResultPage<SysUserResourceListDTO> listSysUserResource(SysUserResourceListQuery query) {
        List<SysUserResourceListDTO> data = new ArrayList<>();
        List<SysUserResource> list = sysUserResourceDAO.selectList(query);
        Optional.ofNullable(list).orElse(Lists.newArrayList())
                .stream().forEach(item->{
            SysUserResourceListDTO d = new SysUserResourceListDTO();
            d.setM(MapperFacadeUtils.map(item,SysUserResourceListDTO.SysUserResource.class));
            data.add(d);
        });
        return new ResultPage(ResultConstant.SUCCESS,data,data.size());
    }

    /*=================================*/
    /*============ SysRole =============*/
    /*=================================*/
    @Override
    public ResultPage<SysRoleListDTO> listSysRole(SysRoleListQuery query) {
        List<SysRoleListDTO> data = new ArrayList<>();
        List<SysRole> list = sysRoleDAO.selectList(query);
        Optional.ofNullable(list).orElse(Lists.newArrayList())
                .stream().forEach(item->{
            SysRoleListDTO d = new SysRoleListDTO();
            d.setM(MapperFacadeUtils.map(item,SysRoleListDTO.SysRole.class));
            data.add(d);
        });
        return new ResultPage(ResultConstant.SUCCESS,data,data.size());
    }

    @Override
    public ResultMsg<SysRoleDetailDTO> findSysRole(Integer id) {
        SysRoleDetailDTO d = new SysRoleDetailDTO();
        SysRole sysRole = sysRoleDAO.selectById(id);
        d.setM(MapperFacadeUtils.map(sysRole,SysRoleDetailDTO.SysRole.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,d);
    }

    @Override
    public ResultMsg<SysRoleSaveDTO> addSysRole(SysRoleSaveDTO saveDTO) {
        Integer id = sysRoleDAO.insertOne(MapperFacadeUtils.map(saveDTO.getM(),SysRole.class));
        saveDTO.getM().setId(id);
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg<SysRoleSaveDTO> editSysRole(SysRoleSaveDTO saveDTO) {
        sysRoleDAO.updateOne(MapperFacadeUtils.map(saveDTO.getM(),SysRole.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg deleteSysRole(Integer id) {
        sysRoleDAO.deleteOne(id);
        return new ResultMsg(ResultConstant.SUCCESS);
    }


    /*=================================*/
    /*============ SysOrgan =============*/
    /*=================================*/
    @Override
    public ResultPage<SysOrganListDTO> listSysOrgan(SysOrganListQuery query) {
        List<SysOrganListDTO> data = new ArrayList<>();
        List<SysOrgan> list = sysOrganDAO.selectList(query);
        Optional.ofNullable(list).orElse(Lists.newArrayList())
                .stream().forEach(item->{
            SysOrganListDTO d = new SysOrganListDTO();
            d.setM(MapperFacadeUtils.map(item,SysOrganListDTO.SysOrgan.class));
            data.add(d);
        });
        return new ResultPage(ResultConstant.SUCCESS,data,data.size());
    }

    @Override
    public ResultMsg<SysOrganDetailDTO> findSysOrgan(Integer id) {
        SysOrganDetailDTO d = new SysOrganDetailDTO();
        SysOrgan sysOrgan = sysOrganDAO.selectById(id);
        d.setM(MapperFacadeUtils.map(sysOrgan,SysOrganDetailDTO.SysOrgan.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,d);
    }

    @Override
    public ResultMsg<SysOrganSaveDTO> addSysOrgan(SysOrganSaveDTO saveDTO) {
        Integer id = sysOrganDAO.insertOne(MapperFacadeUtils.map(saveDTO.getM(),SysOrgan.class));
        saveDTO.getM().setId(id);
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg<SysOrganSaveDTO> editSysOrgan(SysOrganSaveDTO saveDTO) {
        sysOrganDAO.updateOne(MapperFacadeUtils.map(saveDTO.getM(),SysOrgan.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg deleteSysOrgan(Integer id) {
        sysOrganDAO.deleteOne(id);
        return new ResultMsg(ResultConstant.SUCCESS);
    }


    /*=================================*/
    /*============ SysDepartment =============*/
    /*=================================*/
    @Override
    public ResultPage<SysDepartmentListDTO> listSysDepartment(SysDepartmentListQuery query) {
        List<SysDepartmentListDTO> data = new ArrayList<>();
        List<SysDepartment> list = sysDepartmentDAO.selectList(query);
        Optional.ofNullable(list).orElse(Lists.newArrayList())
                .stream().forEach(item->{
            SysDepartmentListDTO d = new SysDepartmentListDTO();
            d.setM(MapperFacadeUtils.map(item,SysDepartmentListDTO.SysDepartment.class));
            data.add(d);
        });
        return new ResultPage(ResultConstant.SUCCESS,data,data.size());
    }

    @Override
    public ResultMsg<SysDepartmentDetailDTO> findSysDepartment(Integer id) {
        SysDepartmentDetailDTO d = new SysDepartmentDetailDTO();
        SysDepartment sysDepartment = sysDepartmentDAO.selectById(id);
        d.setM(MapperFacadeUtils.map(sysDepartment,SysDepartmentDetailDTO.SysDepartment.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,d);
    }

    @Override
    public ResultMsg<SysDepartmentSaveDTO> addSysDepartment(SysDepartmentSaveDTO saveDTO) {
        Integer id = sysDepartmentDAO.insertOne(MapperFacadeUtils.map(saveDTO.getM(),SysDepartment.class));
        saveDTO.getM().setId(id);
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg<SysDepartmentSaveDTO> editSysDepartment(SysDepartmentSaveDTO saveDTO) {
        sysDepartmentDAO.updateOne(MapperFacadeUtils.map(saveDTO.getM(),SysDepartment.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg deleteSysDepartment(Integer id) {
        sysDepartmentDAO.deleteOne(id);
        return new ResultMsg(ResultConstant.SUCCESS);
    }
}